package com.wangyonghao.harmonyocrs.slice;

import com.wangyonghao.harmonyocrs.ResourceTable;
import com.wangyonghao.harmonyocrs.dao.UserDao;
import com.wangyonghao.harmonyocrs.pojo.User;
import com.wangyonghao.harmonyocrs.utils.StringUtils;
import com.wangyonghao.harmonyocrs.utils.ToastUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.TextField;
import ohos.agp.window.service.WindowManager;
import ohos.data.DatabaseHelper;

public class RegistAbilitySlice extends AbilitySlice {
    TextField account;
    TextField password;
    TextField repassword;
    TextField name;
    Button regist;
    Button back;
    UserDao userDao;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_regist);
        userDao = new UserDao(new DatabaseHelper(this));
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN |
                WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS |
                WindowManager.LayoutConfig.MARK_TRANSLUCENT_NAVIGATION);
        initComponent();
        event();
    }

    private void event() {
        regist.setClickedListener(e -> {
            String accountText = account.getText().toString();
            String passwordText = password.getText().toString();
            String repasswordText = repassword.getText().toString();
            String nameText = name.getText().toString();
            if (StringUtils.isEmpty(accountText) || StringUtils.isEmpty(passwordText) ||
                    StringUtils.isEmpty(repasswordText) || StringUtils.isEmpty(nameText)) {
                ToastUtils.showDialog(this, "请补全注册信息！");
                return;
            }
            if (!passwordText.equals(repasswordText)) {
                ToastUtils.showDialog(this, "前后密码不一致！");
                return;
            }
            User user = new User(null, accountText, passwordText, nameText, null, null);
            if (userDao.addUser(user) == -1) {
                ToastUtils.showDialog(this, "注册失败，请检查输入的信息！");
                return;
            }
            ToastUtils.showDialog(this, "注册成功,正在返回登陆界面！");
            gotoLogin();
        });
        back.setClickedListener(e -> {
            gotoLogin();
        });
    }

    private void gotoLogin() {
        Intent i = new Intent();
        present(new LoginAbilitySlice(), i);
    }

    private void initComponent() {
        account = (TextField) findComponentById(ResourceTable.Id_regist_account_input);
        password = (TextField) findComponentById(ResourceTable.Id_regist_password_input);
        repassword = (TextField) findComponentById(ResourceTable.Id_regist_repassword_input);
        name = (TextField) findComponentById(ResourceTable.Id_regist_name_input);
        regist = (Button) findComponentById(ResourceTable.Id_regist_regist_button);
        back = (Button) findComponentById(ResourceTable.Id_regist_back_button);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
